<template>
	<view class="brand">
		<uni-forms ref="brandRef" :model="brandFormData" :rules="brandRules" :label-width="100" label-align="left">
			<uni-forms-item label="品牌logo">
				<uni-file-picker v-model="brandFormData.thumb" fileMediatype="image" mode="grid" :limit="1" />
			</uni-forms-item>
			<uni-forms-item label="品牌名称" name="name" required>
				<uni-easyinput type="text" v-model="brandFormData.name" placeholder="请输入品牌名称" />
			</uni-forms-item>
			<uni-forms-item label="商户电话" name="mobile" required>
				<uni-easyinput type="text" v-model="brandFormData.mobile" placeholder="请输入电话" />
			</uni-forms-item>
			<uni-forms-item label="详细地址" name="address">
				<uni-easyinput type="text" v-model="brandFormData.address" placeholder="请输入地址" />
			</uni-forms-item>
			<uni-forms-item label="商户介绍" name="info">
				<uni-easyinput type="textarea" v-model="brandFormData.info" placeholder="请输入介绍" />
			</uni-forms-item>
		</uni-forms>
		<button type="primary" @click="onSubmit">提交</button>
	</view>
</template>

<script>
	const brandCloudObj = uniCloud.importObject("wanshop-mall-brand")
	export default {
		data() {
			return {
				isSubmitting: false,
				brandFormData: {
					thumb: [],
					name: "",
					mobile: "",
					address: "",
					info: ""
				},
				brandRules: {
					name: {
						rules: [{
							required: true,
							errorMessage: "请输入正确的品牌名称"
						}, {
							minLength: 3,
							maxLength: 15,
							errorMessage: "名称不能少于{minLength}位，不能大于{maxLength}"
						}]
					},
					mobile: {
						rules: [{
							required: true,
							errorMessage: "请输入正确的电话"
						}]
					}
				}
			}
		},
		onLoad() {
			this.getBrand()
			this.isManage()
		},
		
			
		methods: {
				getBrand() {
					brandCloudObj.get().then(res => {
						console.log(res);
						if (res.data.length >= 1) {
							this.brandFormData = res.data[0]
						} else {
							console.log("需要新增数据");
						}

					})
				},
				onSubmit() {
					if (this.isSubmitting) {
						return;
					}
					this.isSubmitting = true;

					this.$refs.brandRef.validate().then(res => {
						let arr = this.brandFormData.thumb.map(item => {
							return {
								extname: item.extname,
								url: item.url,
								name: item.name,
								size: item.size
							}
						})
						this.brandFormData.thumb = arr;
						this.addEdit().then(() => {
							// 成功处理
							console.log('操作成功');
						}).catch(error => {
							// 失败处理
							console.error('操作失败:', error);
						}).finally(() => {
							this.isSubmitting = false;
						});

						console.log(res);
					}).catch(err => {
						console.error('输入错误:', err);
						this.isSubmitting = false;
					});
				},
				addEdit() {
					return new Promise((resolve, reject) => {
						if (this.brandFormData._id) {
							brandCloudObj.update(this.brandFormData)
								.then(res => {
									console.log(res);
									uni.showToast({
										title: "修改成功",
										mask: true
									});
									setTimeout(() => {
										uni.navigateBack();
									}, 800);
									resolve(res); // 明确resolve
								})
								.catch(error => {
									console.error("修改失败", error);
									reject(error); // 明确reject
								});
						} else {
							brandCloudObj.add(this.brandFormData)
								.then(res => {
									uni.showToast({
										title: "添加成功"
									});
									setTimeout(() => {
										uni.navigateBack();
									}, 1000);
									resolve(res); // 明确resolve
								})
								.catch(error => {
									reject(error); // 明确reject
								});
						}
					});
				},

			}
		}
</script>

<style lang="scss" scoped>
	.brand {
		padding: 30rpx;
	}
</style>